using System;
using System.Collections.Generic;
using System.Text;
using Mud.Common.Messages;

namespace Mud.Client
{
    public static class MessageHelper
    {

        #region Helper methods

        /// <summary>
        /// Converts the message to other mesage. It throws an error if the message is not the expected
        /// type.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <returns>Null if the message can not be converted to the converted message.</returns>
        public static T AssertIsMessage<T>(IMessage message) where T : class, IMessage
        {
            T converted = message as T;
            if (converted == null)
            {
                string error = string.Format("Expected message of type {0} but the actual message is of type {1}.", typeof(T), message.GetType());
                throw new ArgumentException(error);
            }
            return converted;
        }

        public static string GetErrorMessage(IMessage message)
        {
            return ((MessageResponseError)message).Description;
        }

        #endregion
    }
}
